package com.angis.fx.util;

import java.io.IOException;

import org.xmlpull.v1.XmlPullParserException;

import android.content.Context;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import com.angis.fx.activity.Enforcement;
import com.angis.fx.data.AreaType;
import com.angis.fx.data.ChangsuoType;
import com.angis.fx.handler.BusinessTypeDBHandler;
import com.angis.fx.handler.EconomyTypeDBHandler;

public class SpinnerUtil {
	
	public static void setAreaCodes(Context context, Spinner spinner) throws XmlPullParserException, IOException {
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(context,
				android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		for (AreaType lType : Enforcement.mAreaTypes) {
			if (lType.getParent() == null)
				adapter.add(lType.getName());
		}
//		Spinner lAreaTypeSpinner = (Spinner) findViewById(R.id.csareavalue);
		spinner.setAdapter(adapter);
	}
	
	public static void setChildAreaCodes(Context context, Spinner spinner, String parentName){
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(context,
				android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		for (AreaType lType : Enforcement.mAreaTypes) {
			if (lType.getParent() !=null && lType.getParent().getName().equals(parentName))
				adapter.add(lType.getName());
		}
//		Spinner lAreaTypeSpinner = (Spinner) findViewById(R.id.csareavalue);
		spinner.setAdapter(adapter);
	}

	public static void setChangsuoType(Context context, Spinner spinner) throws XmlPullParserException, IOException {
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(context,
				android.R.layout.simple_spinner_item);
		adapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		for (ChangsuoType lType : Enforcement.mCSTypes) {
			adapter.add(lType.getId());
		}
//		Spinner lCsTypeSpinner = (Spinner) findViewById(R.id.cstypevalue);
		spinner.setAdapter(adapter);
	}
	
	public static void setEconoType(Context context, Spinner spinner) throws XmlPullParserException, IOException {
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(context,
				android.R.layout.simple_spinner_item);
		adapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		for (String lType : EconomyTypeDBHandler.getAll(context)) {
			adapter.add(lType);
		}
//		Spinner lCsTypeSpinner = (Spinner) findViewById(R.id.cstypevalue);
		spinner.setAdapter(adapter);
	}
	
	public static void setBusiWay(Context context, Spinner spinner) throws XmlPullParserException, IOException {
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(context,
				android.R.layout.simple_spinner_item);
		adapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		for (String lType : BusinessTypeDBHandler.getAll(context)) {
			adapter.add(lType);
		}
//		Spinner lCsTypeSpinner = (Spinner) findViewById(R.id.cstypevalue);
		spinner.setAdapter(adapter);
	}

}
